#! /usr/bin/python
# -*- coding: cp1250 -*-

import re
import sys
import glob
import os
import string
import pepe.p_txt.bigtxt as bt
import pepe.p_filesystem.basic as pfb

def srchMetricet(filename):
    ''' 
        import pepe.p_utils.oracle_dmp as odmp
        print odmp.srchMetricet(f)
        print odmp.srchMetricet(f)[9:]
    '''
    result = ''
    returned = False
    for line in bt.lines(filename):
        if returned == True: break
        if re.search('METRICET ', line):
            # remove end of line characters
            line = re.sub('\x0D\x0A', '', line)
            line = re.sub('\x0D', '', line)
            line = re.sub('\x0A', '', line)
            # return only once, there are multiple 
            # METRICET strings in one dmp file
            if returned == False:
                result = line
                returned = True
                
    return result
    
def multiple(pathfmask):
    ''' 
        oracle_dmp C:\\AFT\\backup\\Cyrill_Rawdata\\*.dmp
        oracle_dmp xyz.dmp
        oracle_dmp *.dmp
    '''
    fmask = string.split(pathfmask, '\x5C')[-1]
    fpath = string.join(string.split(pathfmask, '\x5C')[:-1], '\x5C')
    if 0:
        print fmask
        print fpath
    # get list of files on given path, recursively
    fl = pfb.fslist(root=fpath, patterns=fmask, recurse=True, return_folders=False)

    for f in fl:
        result = srchMetricet(f)
        print "%s\t%s\t%s" % (f, result[9:], result)

if __name__ == '__main__':
    ''' '''
    if len(sys.argv) == 2:
        multiple(sys.argv[1])
    else:
        print multiple.__doc__
